package com.ym.b2b.product.controller;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.ym.b2b.commons.conf.B2BConstants;
import com.ym.b2b.commons.conf.WebTokenUser;
import com.ym.b2b.commons.controller.BaseController;
import com.ym.b2b.product.service.ProductService;
import com.ym.b2b.product.sqlbean.EmBProduct;
import com.zc.commons.base.Page;
import com.zc.commons.base.Result;

@Controller
@RequestMapping("/product")
public class ProductController extends BaseController {

    private Logger                logger = Logger.getLogger(getClass());

    @Autowired
    private ProductService        productService;

    /**
     * @Title: searchProduct
     * @Description: 前台 商品搜索 列表界面
     */
    @RequestMapping("searchProduct.s")
    public ModelAndView searchProduct(EmBProduct emBProduct, Page<EmBProduct> page, HttpServletRequest req) {
        ModelAndView m = new ModelAndView();
        try {
            WebTokenUser tokenUser = getWebTokenUser(req);
            if (tokenUser != null) {
                emBProduct.setUser(tokenUser);
                if(tokenUser.getBuyProperty() != null && !"".equals(tokenUser.getBuyProperty().trim())){
                	if("安徽省".equals(tokenUser.getBuyProperty())){
                		emBProduct.setIsXX("省外禁销");			//	客户限销字段说明为安徽省,产品的限销字段是省外禁销,客户可以看到
                	}else{
                		emBProduct.setIsXX("专销省外");			//	客户限销字段说明为其他,产品的限销字段是专销省外,客户可以看到
                	}
                }
            }else{
            	// 未登录,全禁
            	emBProduct.setIsXX("全禁");
            }

            page.setPageable("true");
            page.setPageUrl(req);
            page.setPageSize(30);
            Result result = new Result();
            emBProduct.setApproveStatus(10);
            emBProduct.setIsSale(1);
/*			if ("NEW_PRODUCT".equals(emBProduct.getOrderByType())) {
			       SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");      
			       Calendar lastDate = Calendar.getInstance();  
			       lastDate.add(Calendar.MONTH,-6);//减一个月，变为下月的1号  
			       emBProduct.setDenglrq(sdf.format(lastDate.getTime()));
			}*/
            result = productService.searchPageProduct(emBProduct, page);
            result.putData("svo", emBProduct);
            m.addObject("result", result);
            m.setViewName("product/product_list_xc");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            m.addObject("msg", e.getMessage());
            m.setViewName(B2BConstants.WEB_ERROR);
        }
        return m;
    }

    /**
     * 获取推荐品种信息
     * 
     * @Title: searchCommendProduct
     * @Description: TODO
     * @Param @param emBProduct
     * @Param @param page
     * @Param @param req
     * @Param @return
     * @Return ModelAndView
     * @Throws
     */
    @RequestMapping("searchCommendProduct.s")
    public ModelAndView searchCommendProduct(EmBProduct emBProduct, Page<EmBProduct> page, HttpServletRequest req) {
        ModelAndView m = new ModelAndView();
        try {
            WebTokenUser tokenUser = getWebTokenUser(req);
            if (tokenUser != null) {
                emBProduct.setUser(tokenUser);
            }
            page.setPageable("true");
            page.setPageUrl(req);
            page.setPageSize(30);
            Result result = new Result();
            emBProduct.setApproveStatus(10);
            emBProduct.setIsSale(1);
            emBProduct.setIsCommend(1);
//            emBProduct.setOrderByType("ALL");	//	仅显示有库存
            result = productService.searchPageProduct(emBProduct, page);
            result.putData("svo", emBProduct);
            m.addObject("result", result);
            m.setViewName("product/commend_product_index_xc");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            m.addObject("msg", e.getMessage());
            m.setViewName(B2BConstants.WEB_ERROR);
        }
        return m;
    }

    /**
     * 获取促销品种信息 展示 没有实际业务
     * 
     * @Title: searchPromoProduct
     * @Param @param emBProduct
     * @Param @param page
     * @Param @param req
     * @Param @return
     * @Return ModelAndView
     * @Throws
     */
    @RequestMapping("searchPromoProduct.s")
    public ModelAndView searchPromoProduct(EmBProduct emBProduct, Page<EmBProduct> page, HttpServletRequest req) {
        ModelAndView m = new ModelAndView();
        try {
            WebTokenUser tokenUser = getWebTokenUser(req);
            if (tokenUser != null) {
                emBProduct.setUser(tokenUser);
            }
            page.setPageable("true");
            page.setPageUrl(req);
            Result result = new Result();
            emBProduct.setApproveStatus(10);
            emBProduct.setIsSale(1);
            emBProduct.setIsPromo(1);
            result = productService.searchPageProduct(emBProduct, page);
            result.putData("svo", emBProduct);
            m.addObject("result", result);
            m.setViewName("product/promo_product_index_xc");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            m.addObject("msg", e.getMessage());
            m.setViewName(B2BConstants.WEB_ERROR);
        }
        return m;
    }

    @RequestMapping("searchPageProduct.s")
    @ResponseBody
    public Result searchPageProduct(EmBProduct emBProduct, Page<EmBProduct> page, HttpServletRequest req) {
        Result result = new Result();
        try {
            emBProduct.setApproveStatus(10);
            emBProduct.setIsSale(1);
            result = productService.searchPageProduct(emBProduct, page);
            return result;
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    /**
     * @Title: searchAjaxProductList
     * @Description: 前台 ajax联想搜索
     */
    @RequestMapping("searchAjaxProductList.s")
    @ResponseBody
    public Result searchAjaxProductList(EmBProduct emBProduct, Page<EmBProduct> page) {
        Result result = new Result();
        try {
            page.setPage(1);
            page.setPageSize(10);
            emBProduct.setApproveStatus(10);
            emBProduct.setIsSale(1);
            result = productService.searchPageProduct(emBProduct, page);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }

    /**
     * @Title: showProductDetail
     * @Description: 商品 详情
     */

    @RequestMapping("showProductDetail.s")
    public ModelAndView showProductDetail(String productCode, HttpServletRequest req) {
        ModelAndView m = new ModelAndView();
        try {
            WebTokenUser tokenUser = getWebTokenUser(req);
            EmBProduct emBProduct = new EmBProduct();
            emBProduct.setProductCode(productCode);

            if (tokenUser != null) {
                emBProduct.setUser(tokenUser);
                if(tokenUser.getBuyProperty() != null && !"".equals(tokenUser.getBuyProperty().trim())){
                	if("安徽省".equals(tokenUser.getBuyProperty())){
                		emBProduct.setIsXX("省外禁销");			//	客户限销字段说明为安徽省,产品的限销字段是省外禁销,客户可以看到
                	}else{
                		emBProduct.setIsXX("专销省外");			//	客户限销字段说明为其他,产品的限销字段是专销省外,客户可以看到
                	}
                }
            }else{
            	// 未登录,全禁
            	emBProduct.setIsXX("全禁");
            }
            Result result = new Result();
            result = productService.showProductDetail(emBProduct);

            m.addObject("result", result);
            if (result.getCode().equals(Result.CODE_100)) {
                m.setViewName(B2BConstants.WEB_ERROR);
                return m;
            }

            m.setViewName("product/product_detail_xc");
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            m.addObject("msg", e.getMessage());
            m.setViewName(B2BConstants.WEB_ERROR);
        }
        return m;
    }

    /**
     * @Title: searchAjaxProductList
     * @Description: 前台 ajax联想搜索
     */
    @RequestMapping("searchSimpleProductList.s")
    @ResponseBody
    public Result searchSimpleProductList(EmBProduct emBProduct, Page<EmBProduct> page) {
        Result result = new Result();
        try {
            emBProduct.setApproveStatus(10);
            emBProduct.setIsSale(1);
            result = productService.searchSimpleProductList(emBProduct);
        } catch (Exception e) {
            result.setCodeResult(Result.CODE_100, e.getMessage());
        }
        return result;
    }
}
